"""
Undead Scourge - Steven Hartin (freddukes)
"""
import es, random, gamethread, wcs
from wcs import wcs
 
RaceName = 'Undead Scourge'
race = wcs.Race(RaceName)

race.registerSkill('Vampiric Aura',5,2,"Draw Health From the Damage you do to Others")
race.registerSkill("Unholy Aura",6,2,"Speed Boost")
race.registerSkill("Levitation",6,3,"Lower Gravity") 
race.registerUltimate("Suicide Bomber",8,8,4,"Hold down the ultimate key to build up the destruction power")
 
def player_spawn(event_var):
    userid = event_var['userid']
    UnholyAura_level = wcs.GetLevel(userid,RaceName,'Unholy Aura')
    if UnholyAura_level:
        plevel = UnholyAura_level / 15.0 + 1
        player = wcs.Command(userid)
        player.Speed(plevel)
        es.tell(userid,'#multi','#lightgreenUnholy Aura #default- #greenYou have gained a %.0f%% speed boost'%((plevel - 1) * 100))
    Levitation_level = wcs.GetLevel(userid,RaceName,'Levitation')
    if Levitation_level:
        depth = 1 - Levitation_level / 20.0 
        player = wcs.Command(userid)
        player.SetGravity(depth)
        es.tell(userid,'#multi','#lightgreenLevitation #default- #greenYou break the laws of gravity, and have now %.0f%% of gravity\'s effect'%(depth*100))
    Bomber_level = wcs.GetLevel(userid,RaceName, 'Suicide Bomber')
    if Bomber_level:
    	wcs.Command(userid).UpdateDict('BombDamage', 0)
 
def player_hurt(ev):
    attacker = ev['attacker']
    dmg = int(ev['dmg_health'])
    vampiric = wcs.GetLevel(attacker, RaceName, 'Vampiric Aura')
    if vampiric:
        player = wcs.Command(attacker)
        heal_amount = int(round(dmg*vampiric*.1))
        player.Heal(heal_amount)
        wcs.Effect().Beam1(attacker,ev['userid'],3,0,255,0)
        es.tell(attacker,'#multi','#lightgreenVampiric Aura #default- #greenYou have drawn %s health'%heal_amount)
    	
def player_ultimate_on(event_var):
	global Players
	userid = event_var['userid']
	Bomber_level = wcs.GetLevel(userid,RaceName,'Suicide Bomber')
	if Bomber_level:
		wcs.Command(userid).UpdateDict('BombDamage', 0)
		delay_name = 'delay_%s'%userid
		gamethread.delayedname(0.5, delay_name, Increasedamage, (userid, delay_name) )
		
def Increasedamage(userid, Delayname):
	es.usermsg("create", "damage_increase", "HintText")
	es.usermsg("write","short","damage_increase",-1)
	a = wcs.Command(userid).ReturnDict('BombDamage')
	format = "Damage:\n-----------------------\n"
	if a < 20:
		es.emitsound('player',userid,'buttons/blip2.wav',0.5,0.5)
		wcs.Command(userid).ModDict('BombDamage',1) # + 1 to the BombDamage dict
		while a >= 0:
			a -= 1
			format += "|"
	else:
		format += "* MAX *"
	es.usermsg("write","string","damage_increase",format)
	es.usermsg("send","damage_increase",userid,0)
	es.usermsg("delete","damage_increase")
	gamethread.delayedname(0.5, Delayname, Increasedamage, (userid, Delayname) )
		
def player_death(event_var):
	userid = event_var['userid']
	Bomber_level = wcs.GetLevel(userid,RaceName,'Suicide Bomber')
	if Bomber_level:
		damage = wcs.Command(userid).ReturnDict('BombDamage')
		if not damage: damage = 1
		gamethread.cancelDelayed('delay_%s'%event_var['userid'])
		sound = 'weapons/%s.wav'%(random.choice(['explode3','explode4','explode5','mortar/mortar_explode1','mortar/mortar_explode2','mortar/mortar_explode3']))
		es.emitsound('player', userid, sound, 1.0, 0.0)
		wcs.Effect().Smoke(userid)
		es.cexec(userid,'-ultimate')
		
def player_ultimate_off(event_var):
	if not int(es.getplayerprop(event_var['userid'],'CCSPlayer.baseclass.pl.deadflag')):
		userid = event_var['userid']
		player = wcs.Command(userid)
		Bomber_level = wcs.GetLevel(userid,RaceName,'Suicide Bomber')
		if Bomber_level:
			gamethread.cancelDelayed('delay_%s'%userid)
			damage = wcs.Command(userid).ReturnDict('BombDamage')
			p = wcs.Command(userid)
			if int(event_var['es_userteam']) == 2:
				p.AreaDamage('#c',Bomber_level * 20 * (damage/5), int(Bomber_level * 7.5 * (damage/5)))
			else:
				p.AreaDamage('#t',Bomber_level * 20 * (damage/5), int(Bomber_level * 7.5 * (damage/5)))
			wcs.Effect().Smoke(userid)
			sound = 'weapons/%s.wav'%(random.choice(['explode3','explode4','explode5','mortar/mortar_explode1','mortar/mortar_explode2','mortar/mortar_explode3']))
			es.emitsound('player', userid, sound, 1.0, 20.0/damage - 1)
			es.sexec(userid,'kill')
